let currSwiperIndex = 0;
const swiperBox = document.getElementById('swiperBox');
const swiperPointBox = document.getElementById('swiperPointBox');

addSwiper();
// resizeSwiperHeight();

/**
 * 添加banner结构
 */
function addSwiper() {
  const lang = localStorage.getItem('lang');
  let swiperList = [];
  if (lang === 'zh') {
    swiperList = [
      '/images/index/home_banner_ch.png',
      '/images/index/home_banner_en_02.png'
    ];
  } else {
    swiperList = [
      '/images/index/home_banner_en_01.png',
      '/images/index/home_banner_en_02.png'
    ];
  }
  let swiperStr = '';
  let swiperPointStr = '';
  for (let i = 0; i < swiperList.length; i++) {
    const currImg = swiperList[i];
    swiperStr += `<img class="ha-swiper-item ${i === currSwiperIndex ? 'active' : ''}" src="${currImg}">`
    swiperPointStr += `<span class="ha-swiper-point-item ${i === currSwiperIndex ? 'active' : ''}"></span>`
  }
  swiperBox.innerHTML = swiperStr;
  swiperPointBox.innerHTML = swiperPointStr;
  setTimeout(() => {
    resizeSwiperHeight()
  }, 10);
}

let swiperTimer = setInterval(() => {
  const swiperChildren = swiperBox.childNodes;
  const swiperPointChildren = swiperPointBox.childNodes;
  const len = swiperChildren.length;
  if (currSwiperIndex < len - 1) {
    currSwiperIndex++;
  } else {
    currSwiperIndex = 0;
  }

  for (let i = 0; i < len; i++) {
    if (i === currSwiperIndex) {
      swiperChildren[i].classList.add('active');
      swiperPointChildren[i].classList.add('active');
    } else {
      swiperChildren[i].classList.remove('active');
      swiperPointChildren[i].classList.remove('active');
    }
  }
}, 3000);

function resizeSwiperHeight() {
  let swiperHeight = document.querySelector('.ha-swiper-item').offsetHeight;
  console.log(swiperHeight);
  document.querySelector('.ha-swiper-container').style.height = swiperHeight + 'px';
}

window.addEventListener('setItemEvent', () => {
  setTimeout(() => {
    addSwiper();
  }, 500)
});


function resizeContentHeight() {
  // 获取内容高度
  let contentHeight1 = document.querySelectorAll('.ha-main-content-text')[0].offsetHeight;
  document.querySelectorAll('.ha-main-item')[0].style.height = contentHeight1 + 'px';
  console.log(contentHeight1+ 'px');
  
  let contentHeight2 = document.querySelectorAll('.ha-main-content-text')[1].offsetHeight;
  document.querySelectorAll('.ha-main-item')[1].style.height = contentHeight2  + 'px';

  let contentHeight3 = document.querySelectorAll('.ha-main-content-text')[2].offsetHeight;
  document.querySelectorAll('.ha-main-item')[2].style.height = contentHeight3 + 'px';
}
  window.addEventListener("resize", ()=>{
      resizeContentHeight()
  });
  
  window.onload = function(){
			resizeContentHeight()
  }

// TODO: 未做节流
window.addEventListener('resize', () => {
  resizeSwiperHeight();
});




